package com.example.parking.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.parking.common.PageRequest;
import com.example.parking.common.Result;
import com.example.parking.entity.ParkingLot;
import com.example.parking.service.ParkingLotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/parking-lot")
public class ParkingLotController {

    @Autowired
    private ParkingLotService parkingLotService;

    @PostMapping
    public Result<ParkingLot> create(@RequestBody ParkingLot parkingLot) {
        parkingLotService.save(parkingLot);
        return Result.success(parkingLot);
    }

    @PutMapping("/{id}")
    public Result<ParkingLot> update(@PathVariable Long id, @RequestBody ParkingLot parkingLot) {
        parkingLot.setId(id);
        parkingLotService.updateById(parkingLot);
        return Result.success(parkingLot);
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        parkingLotService.removeById(id);
        return Result.success(null);
    }

    @GetMapping("/list")
    public Result<?> listAll() {
        return Result.success(parkingLotService.list());
    }

    @GetMapping("/{id}")
    public Result<ParkingLot> getById(@PathVariable Long id) {
        return Result.success(parkingLotService.getById(id));
    }

    @GetMapping("/page")
    public Result<Page<ParkingLot>> page(PageRequest pageRequest, ParkingLot parkingLot) {
        Page<ParkingLot> page = new Page<>(pageRequest.getPageNum(), pageRequest.getPageSize());
        LambdaQueryWrapper<ParkingLot> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(parkingLot.getName() != null, ParkingLot::getName, parkingLot.getName())
                .like(parkingLot.getAddress() != null, ParkingLot::getAddress, parkingLot.getAddress())
                .eq(parkingLot.getStatus() != null, ParkingLot::getStatus, parkingLot.getStatus());
        return Result.success(parkingLotService.page(page, wrapper));
    }
} 